<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/18
 * Time: 17:17
 */

namespace app\api\controller;


use app\api\model\DeviceJpush as DeviceJpush;
use app\api\model\Push as PushModel;
use app\api\service\User as UserService;
use think\Db;

class Push
{
    //添加设备号
    public function addDevice()
    {
        $jpush_reg_id = input('post.jpush_reg_id');
        $device_no = input('post.device_no');
        $device_type = input('post.device_type');
//        $device = Db::name('device_jpush');
        $info = DeviceJpush::get(['device_no' => $device_no]);
        if (!$info) {
            $data = [
                'device_no' => $device_no,
                'jpush_reg_id' => $jpush_reg_id,
                'device_type' => $device_type,
            ];
            $res = DeviceJpush::create($data);
        } else {
            if (!empty($jpush_reg_id)) {
                $info->jpush_reg_id = $jpush_reg_id;
                $res = $info->save();
            }
        }
        return json(['errorCode' => 0, 'msg' => '成功']);
    }


    //推送记录
    public function getList()
    {
        $user_id = UserService::getUserIdByToken();
        $list = PushModel::where('user_id', $user_id)->order('add_time desc')->select();
        return json(['errorCode' => 0, 'data' => $list]);
    }


}